package com.apache.plateform.tomcat;

import com.apache.rpc.StartSocketPortListener;
import com.apache.rpc.common.RpcUtil;
import com.apache.tools.StrUtil;
import com.apache.uct.common.filter.LicenseFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * FilterConfiguration<br>
 * @Author IUS platform administrators create generation
 * @Date 2018-11-16 14:05:35
 */
@Configuration
public class FilterConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private Environment env;

    @Bean
    public StartSocketPortListener startSocketPortListener() {
        String socketPort = StrUtil.doNull(env.getProperty("rpc.service.port"), "20808");
        StartSocketPortListener listener = new StartSocketPortListener(socketPort);
        if (StrUtil.isNotNull(env.getProperty("rpc.version"))) {
            RpcUtil.getInstance().setVersion(env.getProperty("rpc.version"));
        }
        return listener;
    }

    @Bean
    public FilterRegistrationBean encodingFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CharacterEncodingFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("encoding", "UTF-8");
        registration.addInitParameter("forceEncoding", "true");
        registration.setName("encodingFilter");
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean defaultBaseSecurityFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new LicenseFilter());
        registration.addUrlPatterns("/*");
        registration.setName("LicenseFilter");
        registration.setOrder(2);
        return registration;
    }

}
